package com.jintian.smart.kernel.job.config;

import com.jintian.smart.kernel.autoconfigure.beetl.EnableBeetlMapper;
import com.jintian.smart.kernel.core.job.SmartJob;
import com.jintian.smart.kernel.job.job.SmartJobLauncher;
import com.jintian.smart.kernel.job.mapper.SmartJobRepository;
import org.redisson.api.RedissonClient;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;

/**
 * 定时任务 配置
 */
@Configuration
@EnableConfigurationProperties(SmartJobConfig.class)
@ConditionalOnProperty(prefix = SmartJobConfig.CONFIG_PREFIX, name = "enabled", havingValue = "true", matchIfMissing = true)
@EnableBeetlMapper(basePackages = {"com.jintian.smart.kernel.job.mapper"}, sqlManager = "sqlManager", mapperSuffix = "Mapper")
public class SmartJobAutoConfiguration {
    private final SmartJobConfig jobConfig;

    private final SmartJobRepository jobRepository;

    private final List<SmartJob> jobInterfaceList;

    public SmartJobAutoConfiguration(SmartJobConfig jobConfig, SmartJobRepository jobRepository, List<SmartJob> jobInterfaceList) {
        this.jobConfig = jobConfig;
        this.jobRepository = jobRepository;
        this.jobInterfaceList = jobInterfaceList;
    }

    /**
     * 定时任务启动器
     *
     * @return
     */
    @Bean
    public SmartJobLauncher initJobLauncher(RedissonClient redissonClient) {
        return new SmartJobLauncher(jobConfig, jobRepository, jobInterfaceList, redissonClient);
    }
}
